Change Your Address By Neil J. Rubenking, PC Magazine Download: coa2.zip When you install a program in Windows, the system builds a web of connections that makes moving the program very difficult. If disk space constraints force a move, or if adding a new device causes drive letters to change, the system can lose track of essential files. References to the program are stored in shortcuts, INI files, and the system Registry. COA2, an update of our Change of Address utility, tracks down all references to the old address and replaces them with the new address. When the changes are complete, the utility presents you with a list of changes and gives you the option to undo any of them. Note that COA2 does not actually move any files. It reports moves and name changes to the system. This new version offers Windows 2000 support and an improved user interface. COA2 runs under Windows 95, Windows 98, Windows Me, Windows NT 4.0, and Windows 2000. The Delphi 5 source code is provided with the utility for those interested in seeing how it works. Note that PC Magazine programs are copyrighted and cannot be distributed, whether modified or unmodified. Use is subject to the terms and conditions of the license agreement distributed with the programs. Using COA2 To install COA2, run the supplied installation program, Install.exe. To uninstall the program, use the Add/Remove Programs applet in the Windows Control Panel. Before we go into the details of how to use COA2, let's consider a few scenarios in which it can help. Running out of room on one drive and having another drive brimming with free space is not uncommon. You can remedy this imbalance by locating a folder that's taking up a lot of space on the crowded drive and moving it to the drive that has more space. Less frequently, you may gain space by moving one immense file rather than an entire folder. Making the move is simple. In Windows Explorer, right-drag the folder or file from the old location to the new and select Move here from the menu that appears. For a more cautious approach, you can select Copy here from that menu and wait to delete the original until after you've successfully updated the system references. Changing all references to reflect the new address used to be hard, but COA2 makes the process easy. Fill in the old and new addresses in COA2's first screen ( Figure 1 ). COA2 requires that you use an existing folder or file for the new address. This means you can browse for the new address by clicking the folder or file buttons adjacent to the new address line. If you copied the folder or file, as opposed to moving it, you can also browse for the old address. If the old address no longer exists, just type it in, making sure to include a final backslash if it's a folder. When you click the Change now button, COA2 updates all system references that use the old address. Sometimes, when you add a new device, it takes over a drive letter that's in use, pushing existing drives to a higher letter. This also can happen when you use a partitioning utility to create a logical drive. Any system reference to a file or folder on a drive whose letter has changed will become invalid. If only one drive was affected, you simply enter the old and new drive letters, each followed by a colon. If adding the new drive letter caused multiple drive letters to change, you must make the changes in the correct order. When multiple drive letters have been changed, you must always change the last drive letter first. For example, suppose drives D:, E:, and F: changed to E:, F:, and G:. You would use COA2 to change F: to G:, then to change E: to F:, and once more to change D: to E:. In the same way, if you remove a device or partition, the disappearance of its drive letter will pull other drives down to previous letters, invalidating system references to files and folders on the changed drives. Here too, if only one drive was affected, you can just enter the old and new addresses. But if multiple drive letters changed it's extremely important to make the changes in the correct order. When multiple drive letters have moved to lower letters, you must always change the first drive letter first. Suppose you need to change drives E:, F: and G: to D:, E:, and F:. You would use COA2 to change E: to D:, again to change F: to E:, and once more to change G: to F Some partitioning utilities let you merge existing partitions without losing data. They may even include an option to update system references as COA2 does. If you use a partitioning utility that doesn't include this feature, you'll need to move a partition's folders to other partitions before deleting the old partition. In that case, you would first move the folders, then use COA2 to inform the system of each move. Next, eliminate the now-empty partition and use COA2 to notify the system of any changes to other drive letters. Tips And Details Now we'll look at COA2 in more detail. As you saw in Figure 1, COA2's first screen invites you to fill in the Old address and New address fields. Both of these must be valid filenames, and the new address must represent an existing file or folder. You can click the folder button adjacent to either address line to browse for a folder, or click the file button to browse for a file. Often, the two addresses will be nearly the same, in which case you can use the Copy down and Copy up buttons to copy one address into the other for editing. You can also enter a Description for the change record, or click the Fill button to automatically generate one. If you leave the description blank, COA2 will generate it for you. As you enter the old and new addresses, the Status line indicates Not ready, Ready, or Warning, along with a short explanation. The adjacent status button displays a red, green, or yellow light. A red light means you can't proceed, and a green light means everything is fine. The Warning status with a yellow light means that the old and new addresses are valid, but there may be a problem. Click the status button for a detailed explanation of the current status. You'll find all the possibilities described on the Address status page in the online help. When you click the Change now button, COA2 starts the process of changing the old address to the new wherever it's found. Every filename has a short filename equivalent. Short filenames are all uppercase, with no more than eight characters in the filename portion and three characters in the extension. The name is based on the long filename and the other short filenames that already are in use on your system. The rules that Windows uses to determine the short filename can be found in COA2's help file. System references occasionally use the short filename, so COA2 does its best to update the short filename as well as the long filename. Because COA2 requires that the new filenames you specify must already exist, finding their short filename equivalents is a simple matter of asking Windows. But sometimes the old filenames no longer exist when you run COA2. If the old filename doesn't exist, COA2 attempts to temporarily create the file so it can obtain the short filename that Windows assigns. This attempt will fail if the old address is on a drive letter that's nonexistent or read-only. In that rare instance, COA2 will present you with the Short filenames screen ( Figure 2 ). Here you can type in the short filename, using the standard Windows rules to deduce it. If you're not sure what to enter, check the Don't use short filenames box. The option to ignore short filenames applies only to the current change. When you're done entering information, click Change now. If you reached the Short filenames screen because you mistyped one of the addresses, click Back to go back to the initial screen. During the search and replace process, the Changing addresses screen ( Figure 3 ) keeps you apprised of COA2's progress. The figures in the first column represent the number of shortcut files, INI files, and Registry keys searched. Those in the second column represent the number of actual changes. Multiple changes may occur within a given file or key, so the number in the second column may be larger than the one in the first. In addition to the changing numeric displays, a small animation indicates that the program is active. COA2 changes its cursor to an hourglass as a reminder that you must not interrupt this process. If you try to close the program while it's busy, it will politely refuse. The process shouldn't take more than a few minutes. When the update process is complete, COA2 displays the Review change records screen ( Figure 4 ). Each line in the list represents one change in a shortcut, an INI file, or the Registry, listed in the order they were performed. The first column holds the filename or Registry key name, the second holds the data string containing the old address, and the third holds the data string after the change of address. When you hover the mouse over a line, you'll get a floating ToolTip with more detail. Each line in the list of changed items starts with two icons. The right-hand icon indicates the type of system reference in which the change took place: a boxed shortcut arrow for shortcuts, a Regedit icon for the Registry, and the letters INI for INI files. The left-hand icon is usually a check box, but in Figure 4, we contrived several problem situations to display all of the possibilities for this icon. If COA2 was unable to make the specified change, a red circle icon, indicating an error, replaces the check box. If the old address was a type that's not safe for COA2 to change, a yellow triangle icon replaces the check box to warn you. When you point the cursor at a line with an error or warning icon, the ToolTip that appears will provide more information about what went wrong. It's possible for the change operation to have unwanted side effects, especially if you ignored a status warning in the initial screen. For example, if you changed the folder name C:\FOO to C:\BAR, ignoring the warning that folder names should end with a slash, you might accidentally change C:\FOOD to C:\BARD. Review each item carefully, and if any of them proves to be an error, select its check box and click Undo checked. You can click the Check all button to check every item, or Check none to uncheck them all. When you click Undo checked, COA2 does its best to undo all of the checked items. It starts at the bottom of the list, undoing the most recent items first. If the undo operation for an item succeeds, a green-square icon representing success replaces the check box. If it fails, a red-square icon representing failure replaces the check box. For more information about what went wrong, point at the line with the failure icon to bring up the ToolTip. You can review the most recent records, up to nine of them, using the Next record and Previous record buttons. For a text or HTML-format report of exactly what was changed and details on any problems, click the Create detail report button. In either case, COA2 will preview the report for you in its Detail report screen ( Figure 5 ). To print the report, click Launch to load it into the associated application (usually Notepad for text reports and your browser for HTML. Note that systems without Microsoft Internet Explorer 4 or later will not be able to preview the HTML report; COA2 will alert you when this is the case. You can still launch the report and view it in your browser. Once you've undone any items that needed undoing and created your detailed report, if desired, the change record has served its purpose. COA2 discards the oldest change records, retaining only the nine most recent ones, but you don't have to wait. If you're completely finished with a change record, you can click Delete record to delete it immediately. COA2'S Reminders COA2 is a powerful program. Used incorrectly, it could cause precisely the same problem it's designed to fix. To minimize the possibility of accidental misuse, the program incorporates a number of first-time reminder messages. Each of them includes a check box that lets you suppress that particular message in the future. The first time you launch COA2, you'll get a reminder that the program does not actually move any files. Our experience with COA2's predecessor made it clear that many users need this reminder. When you click the Change now button, COA2 immediately begins searching for the old address and replacing it with the new one. This behavior is different from that of the previous version, so the very first time you click the Change now button, you'll get a reminder of what's about to happen. You'll probably suppress these two warnings after you've seen them once or twice. Any time you click Change now despite a Warning status, you'll be reminded to click the status button and review the information displayed. When you use COA2 to change a drive letter (D:, for example) or root directory (D:\), COA2 will remind you to change multiple drive letters in the correct order. These two reminders are important, so you'll probably want to leave them in place. If you've suppressed some of the reminders and want to restore them, locate the file Coa2.ini in the same folder as the COA2 program and delete it. That will restore all the reminders and reset a handful of other settings to their default values. Inside COA2 COA2 has a substantially simpler user interface than its predecessor. It avoids asking the user unnecessary questions and provides an undo feature in lieu of asking detailed questions before confirming changes. It takes advantage of the folder-browsing dialog supplied by Windows, including advanced features available only in specific versions of the Windows shell. Windows won't supply the short filename for a nonexistent file or folder, but COA2 manages to get around that limitation in most cases. COA2 also takes advantage of Delphi5's form-scaling feature, but the program required workarounds to avoid conflicts with other Delphi5 features. All these topics will be discussed in the section that follows. Just Do It COA2 is actually the third version of this utility. The original COA was a 16-bit utility for Windows 3.1. The first update to COA, COA32, added support for the 32-bit Windows platforms that were available at the time: Windows 95, Windows NT 3.51, and Windows NT 4.0. Windows 3.51 has fallen out of usage, and some of the new Registry data types in Windows 98, Windows Me, and Windows 2000 caused COA32 to freeze up. That problem was the main impetus for this upgrade, but at the same time, we decided to streamline and enhance the user interface. COA32 quizzed the user in painful detail as to where it should look for INI files and shortcuts, and what Registry keys it should search. The utility then searched those areas and presented a complicated list of occurrences of the old address. The user was asked to review the list and mark any items that should not be changed. Eventually, COA32 got around to making the specified changes. In contrast, COA2 simply searches all local drives and the entire Registry and makes all the changes it can, preserving the information required to undo each change. It displays the results when the changes are done, and permits the user to undo them individually. Because most sessions will not require any undo operations, this is much easier on the user. Where COA32 had 12 distinct screens, COA2 has just five, and one of them is rarely seen. COA32 also allowed the user to change any text string to any other text string within all the found system references. We decided that was too much power for the program. Allowing the user to change cat to dog throughout the system could be dogastrophic! The program's purpose is to fix system references to files or folders, not to serve as a general-purpose search-and-replace utility. For safety, COA2 requires that the new address be an existing file or folder, and that the old address at least have the form of a valid filename. As a result of these changes, COA2 is both easier and safer to use than COA32. You may want to consider eliminating unnecessary confirmation steps from your own programs, especially if the user will almost always confirm the action. Code the program to take the expected action instead, and leave an option to undo if necessary. Browsing In Style The GetFolder() function defined in COA2's Coafuncs.pas module uses the SHBrowseForFolder() API function to display the familiar folder-selection tree dialog. Using SHBrowseForFolder() isn't as simple as arranging for file selection with a TOpenDialog component, but there are many opportunities to customize the dialog to work exactly as you wish. SHBrowseForFolder() takes a data structure of the Windows-defined type TBrowseInfo as its only argument, and returns a PIDL (pronounced piddle)-a pointer to an Item ID List. Like a pathname, a PIDL identifies a folder or other object within the Windows shell's name space. But where a pathname can refer only to an existing file or folder, a PIDL also can identify a virtual object, such as Control Panel. COA2 deals only with existing files or folders but has to reference them through a PIDL. GetFolder() defines a variable of type TBrowseInfo and initializes its data fields to control the behavior of the folder-selection tree. GetFolder() requests a folder tree rooted at My Computer, specifies the dialog title and starting folder, and identifies a callback procedure that will be used for communication between the dialog and COA2. The ulFlags field of the TBrowseInfo structure holds a large number of bit flags that define specific behaviors for the dialog, some of which are only available under a particular version of the Windows shell. GetFolder() starts with three basic flags that are valid in all shell versions: BIF_RETURNONLYFSDIRS forces the dialog to return only file-system folders, not virtual folders.BIF_DONTGOBELOWDOMAIN prevents the dialog from including network folders below the domain level.BIF_STATUSTEXT provides the dialog with a status line. Version 4.71 of the Windows shell, introduced with IE4, adds the option to include an edit box in the dialog, allowing users to enter full pathnames directly ( Figure 6 ). If the shell version is 4.71 or later, GetFolders() includes the flags BIF_EDITBOX, which adds the edit box, and BIF_VALIDATE, which prevents the dialog from accepting an invalid folder name in the edit box. Version 5.0 of the shell, provided with Windows 2000, Windows Me, and IE5, changes the user interface for the dialog substantially. The new dialog style, invoked using the BIF_NEWDIALOGSTYLE flag, is resizable and supports a number of context-menu commands ( Figure 7 ). The SHBrowseForFolder() function doesn't return until the user clicks OK or Cancel. If the returned PIDL is non-nil, GetFolder() calls the API function SHGetPathFromIDList() to convert the PIDL into a path. Note that both the returned PIDL and the My Computer PIDL obtained earlier must be freed by calling the API function CoTaskMemFree(). DOS Is Dead; Long Live DOS! In ancient days, when DOS ruled the earth, filenames were limited to eight characters and extensions were limited to three characters. Only capital letters were permitted, and more than a dozen punctuation marks were forbidden. All modern Windows platforms support long filenames, with no specific length limitation except that the full pathname must not exceed MAX_PATH (260) characters. System references, however, occasionally still use the equivalent, old-fashioned short filename. When the long filename differs from the corresponding short filename in more than just upper/lower case, COA2 does its best to change both. The short filename is generated by the file system when a file is created. The file system converts all alphabetic characters to uppercase, eliminates banned characters, truncates the filename to eight characters and the extension to three, and replaces the last two or more characters of the filename with a numeric tail-a tilde and a number. The numeric tail uses the first available number that results in a unique filename. For example, suppose the file long filename bonnie.txt exists, and its short filename is LONGFI~1.TXT. If you now create long filename clyde.txt, its short filename will incorporate the next unused numeric tail and become LONGFI~2.TXT. The Windows API function GetShortPathName() takes a long path name and returns its 8.3 equivalent. Because the precise numeric tail depends on the names of existing files in the same folder, this function only works on filenames that exist. In most cases, the GetShort() function, defined in the module Coafuncs.pas, can obtain a short filename even for a long filename that doesn't represent an existing file. It does so by temporarily creating the specified filename, including any parent folders that do not exist. It calls the API function GetShortPathName() to obtain the desired information, then removes each filename that it created. This process is not 100-percent foolproof. It assumes that the long filename did exist in the recent past, and that recreating the long filename will yield the same short filename. Because COA2 is almost always used immediately after moving files or folders, this is a reasonable assumption. Note, too, that GetShort() will fail if recreating the filename is impossible, as when the filename refers to a drive letter that no longer exists. In that situation, COA2 displays the Short filenames screen and asks the user to supply the necessary information. Size Does Matter In a departure from my usual programming style, I designed COA2 to scale according to the user's font size setting in Display Properties. When the user chooses Small fonts, Large fonts, or a custom size, the size of the program's windows, controls, and fonts changes accordingly. This ability is built into Delphi. Enabling the scaling feature should be a simple matter of setting each form's Scaled property to True. The automatic scaling feature, however, can conflict with two other standard Delphi properties: Align and Anchors. The Align property makes a Delphi control stick to one edge of its parent control or form. As the parent is resized, the aligned component changes width or height as necessary so it completely fills the edge of the parent. For example, the Back and Launch buttons on COA2's Detail report page reside within a TPanel whose Align property is set to alBottom. When the form is resized, that panel remains at the bottom. If Align is set to alClient, the control fills the entire client area of the parent except for areas occupied by other aligned controls. The Anchors property provides another means to define a control's behavior when its parent is resized. This property can anchor a control to any or all of the four edges of its parent. By default, controls are anchored to the top and left edges. A control that's anchored to the bottom and right edges will move when the parent is resized so that it maintains the same distance from those two edges. You can even anchor a control to both the right and left edges, in which case it changes width as necessary when the parent is resized. COA2's first screen contains several edit boxes that are anchored to the left and right edges. Unfortunately, form-scaling occurs early in the loading process, before controls are fully initialized, and it can throw off the initial size and position of controls that rely on the Align or Anchors property. To compensate for this conflict, the FormCreate() method of COA2's main form (defined in Mainu.pas) includes a subfunction called PlaceControlsForScale(). For every control whose size is affected by the Align property, PlaceControlsForScale() explicitly sets the correct size, based on the size of the parent. For every control whose size or position is determined by the Anchors property, PlaceControlsForScale() calculates the correct initial size and position. If you use the Align or Anchors properties in conjunction with Delphi's form scaling, you'll probably have to do something similar. In a perfect world, you'd never have to move a program after installing it. In the real world, you have to do so frequently. When you do, make sure to send the system a change of address notice using COA2. Neil J. Rubenking, the author of COA2, is the contributing technical editor of PC Magazine. Sheryl Canter is the editor of the Utilities column and a contributing editor of PC Magazine. < back